$(function () {
    var $datagrid = $("#datagrid"), $toolbar = $("#toolbar"), $dialog=$("#dialog") ;
    $("#saveSeller").on("click",function(){
        openDialog();
    });
    $("#editSeller").on("click",function(){
        var rows = $datagrid.datagrid('getSelected');
        if(rows){
            var url = "form-edit.jsp?sellerId="+rows.Id;
            openDialog(url);
        }else{
            alert('请选择对象');
        }
    })
    $datagrid.datagrid({
        singleSelect: true,
        fit: true,
        border: false,
        fitColumns: true,
        url: Constant.ctx("/mulanweb/admin/seller/search"),
        method: 'post',
        pagination: true,
        toolbar: $toolbar,
        columns: [[
            {field: 'Id', title: '编号'},
            {field: 'SellerName', title: '名称'},
            {field: 'Username', title: '登录名'},
            {field: 'MobileNumber', title: '手机号码',formatter:function(v,r,i){
                if(v){
                    return v ;
                }else{
                    return "未填写";
                }
            }},
            {field: 'Email', title: '邮箱'},
            {field: 'ParentSeller', title: '所属商家主键'},
            {field:'CreateTime',title:'注册时间'}
        ]],
       onBeforeLoad: function (param) {
            param.PageNumber = param.page;
            param.PageSize = param.rows;
           var keyWord = $("#searchKeyWord").val();
           if(keyWord){
               param.keyWord = keyWord;
           }
            return true;
        },
        loadFilter: function (data) {
            var newData = {};
            if (data.ReturnCode == 1) {
                newData.rows = data.DataList;
                if (newData.rows.length > 0) {
                    newData.total = data.TotalRow;
                } else {
                    newData.total = 0;
                }
            }
            return newData;
        }
    });
    function saveAccount() {
        $("form").form("submit", {
            url: $("form").attr("action"),
            onSubmit: function () {
                var isValid = $(this).form('validate');
                if (!isValid) {
                    $.messager.progress('close');	// hide progress bar while the form is invalid
                }
                return isValid;
            },
            success: function (respText) {
                var  resp= $.parseJSON(respText);
                Constant.showMessage(resp.AlertMessage,resp.message, "操作成功");
                if (resp.ReturnCode == 1) {
                    $("#datagrid").datagrid("reload");
                    $dialog.dialog("close");
                }
            }
        });
    }
    function openDialog(url) {
        var tag  = url? true:false;
        $dialog.dialog({
            title: tag?"编辑商家":"新增商家",
            width: 430,
            height: 460,
            closed: false,
            cache: false,
            href: tag?url:'form.jsp',
            modal: true,
            onClose: function () {
                $("#form-dialog").dialog("destroy");
            },
            buttons: [{
                text: '保存',
                handler: saveAccount
            }]
        });
    }
})

function doSearch(){
    $("#datagrid").datagrid("reload");
}